/*
************************************************************************************************************************
	Creates estimation dataset $workdata/baseline with information on:
	- Social assistance (sa)
	- Employment (wage)
	- Earnings (wage2)

	Uses auxiliary datasets:
	- $temp/bef_red2_`year' (see "$data_do/bef_red")
	- $temp/mia_con_`year'  (see "$data_do/mia_con")

	Registers: Variables (Years) 
	- DREAM: pnr, y_01-y_52(53) (2000-2006)

************************************************************************************************************************
*/

forvalues year = 2000/2006 {

***************************************************************
* Setting start dates for each year
***************************************************************

if `year'==2000 local day1=mdy(1,3,2000)
if `year'==2001 local day1=mdy(1,1,2001)
if `year'==2002 local day1=mdy(12,31,2001)
if `year'==2003 local day1=mdy(12,30,2002)
if `year'==2004 local day1=mdy(12,29,2003)
if `year'==2005 local day1=mdy(1,3,2005)
if `year'==2006 local day1=mdy(1,2,2006)


***************************************************************
* Use data with population characteristics
***************************************************************

use "$temp/bef_red2_`year'", clear  /* (see $data_do/bef_red) */

***************************************************************
* Limit sample to include only those with basic education (edul=="10") and ages 19-35 at start of year.
* (to limit computations)
***************************************************************
keep if alderl>=19 & alderl<=35
keep if edul=="10" 

***************************************************************
* Merge with data with employment information (mia_con).
***************************************************************

capture drop _merge
if year <  2008 merge 1:m pnr using "$temp/mia_con_`year'" ,  keep(master match)  /* (see $data_do/mia_con) */

***************************************************************
* Merge with data with weekly public income transfers y_1-y_52(y_53).
***************************************************************

capture drop _merge
merge m:1 pnr using "$rawdata/dream`year'", keep(master match)
preserve
keep y_*
local num_weeks=c(k) /* number of weeks of the year */
restore
display `num_weeks' 
forvalues yvar= 1/9 {
rename y_0`yvar' y_`yvar'
}

***************************************************************
* Calculate weekly age for each week of year.
***************************************************************
forvalues dream_week = 1/`num_weeks' {
g age_`dream_week'= ((`day1'+(7*(`dream_week'))-foed_dag))/365.25
}

***************************************************************
* Create weekly variables of employment (w_1-w_`num_weeks') and earnings (q_1-q_`num_weeks') in corresponding months
***************************************************************
forvalues week = 1/`num_weeks'  {
g w_`week'=0
g q_`week'=0
g month_`week'=0
}

forvalues month = 1/12 {
forvalues week = 1/`num_weeks'  {

if `month'<12 ///
&  age_`week'>= ((mdy(`month',1,`year') - foed_dag)/365.25) ///
&  age_`week'<  ((mdy(`month'+1,1,`year') - foed_dag)/365.25) {
if year <  2008 replace w_`week'=m3_`month'
if year <  2008 replace q_`week'=l2_`month'    
replace month_`week'=`month'
}

if `month'==12 ///
&  age_`week'>= ((mdy(`month',1,`year') - foed_dag)/365.25) ///
&  age_`week'<=  ((mdy(12,31,`year') - foed_dag)/365.25) {
if year <  2008 replace w_`week'=m3_`month'    if year <  2008
if year <  2008 replace q_`week'=l2_`month'    
replace month_`week'=`month'
}
}
}

***************************************************************
* Create weekly observations of employment (w) and earnings (q) and public income transfer (y) in weeks 1-`num_weeks'.
* (wide to long)
* Save baseline_2_`year'
***************************************************************


expand `num_weeks'
sort pnr 
capture drop week
gen week=1
bysort pnr: replace week=sum(week)
capture drop age
gen age=0
capture drop month
gen month=0
capture drop y
gen y=0
capture drop w
gen w=0
capture drop q
gen q=0

forvalues week=1/`num_weeks' {
bysort pnr: replace age=age_`week' if week==`week'
bysort pnr: replace y=y_`week' if week==`week'
bysort pnr: replace w=w_`week' if week==`week'
bysort pnr: replace q=q_`week' if week==`week'
bysort pnr: replace month=month_`week' if week==`week'
}

capture drop m_* 
capture drop m3_*
capture drop l_* 
capture drop l2_* 
capture drop q_* 
 
drop y_* age_* month_* w_*   _merge
save "$temp/baseline_2_`year'", replace
}


********************************************
* Applying selelction criteria to select Baseline sample.
********************************************

forvalues year = 2000/2006 {
use "$temp/baseline_2_`year'", clear

keep if ie_type==1
keep if edul=="10" 
keep if eduf=="10" 
keep if fm_markl==6
keep if fm_markf==6 
keep if d_childl==0 
keep if d_childf==0 
keep if civstl=="U" 
keep if civstf=="U" 

********************************************
* Defining weekly ordinary employment (wage), earnings (wage2), social assistance (sa).
* Save baseline_3_`year'
********************************************

capture drop wage
g wage=0
replace wage=1 if w>0 & w!=.
replace wage=0 if y==651 | y==652 /*Study grant*/
replace wage=0 if y==135 | y==136 | y==145 | y==146  ///
                | y==215 | y==216                    ///
				| y==705 | y==706 | y==715 | y==716  ///
				| y==725 | y==726 | y==735 | y==736  ///
				| y==745 | y==746 | y==755 | y==756 | y==765 | y==766 | y==815 | y==816 | y==875 | y==876  ///
				| y==895 | y==896 /*Wage subsidy program*/  ///
				| y==997          /*Deceaced*/ 


capture drop wage2				
g wage2=0
replace wage2=wage*q

capture drop sa
g sa=0
replace sa=1 if y>=130 & y<=139 ///
              | y>=140 & y<=149 ///
			  | y>=700 & y<=709 ///
			  | y>=710 & y<=719 ///
			  | y>=720 & y<=729 ///
			  | y>=730 & y<=739 /*social assistance*/

save "$temp/baseline_3_`year'", replace
}

***************************************************
*Collect yearly dataset to one.
*Define age in months
*Save baseline
***************************************************

use pnr foed_dag age week month year y sa wage su koen edul eduf d_childl d_childf ie_type fm_markl fm_markf using "$temp/baseline_3_2000", clear
forvalues year=2001/2006 {
append using "$temp/baseline_3_`year'", keep(pnr foed_dag age week month year y sa wage su koen edul eduf  d_childl d_childf ie_type fm_markl fm_markf )
}

keep if year>=2000 & year<=2006
capture drop age_q
g age_q=floor( (age-floor(age))*12)*(1/12) + floor(age)
keep if age_q>=20 & age_q<30
g e=1
replace month=12 if month==0

save "$workdata/baseline", replace